package com.xuxu.blog.controller;

import com.xuxu.blog.dao.ArticleDao;
import com.xuxu.blog.dao.CopyDao;
import com.xuxu.blog.entity.ArticleEntity;
import com.xuxu.blog.entity.Copy;
import com.xuxu.blog.entity.UserEntity;
import com.xuxu.blog.openfeign.ProductClient;
import com.xuxu.blog.service.ArticleService;
import com.xuxu.blog.utils.DateConvertUtil;
import com.xuxu.common.utils.R;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.xuxu.blog.constant.MQPrefixConst.EMAIL_EXCHANGE;

/**
 * @className: TestController
 * @author: Maxuxu
 * @date: 2022/8/30 16:21
 * @description: 测试的控制类
 */
@RestController
public class TestController {
    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    ArticleDao articleDao;
    @Resource
    ArticleService articleService;
    @Resource
    private ProductClient client;
    @Resource
    private CopyDao copyDao;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    RedissonClient redissonClient;

    @GetMapping("/test4")
    public R test4() {
        // 获取一把锁，只要锁的名字一样，就是同一把锁
        RLock lock = redissonClient.getLock("my-lock");
        //    加锁
        //    默认的锁过期时间是30s，也就是如果执行过程中出错导致没有解锁，到期就会自动解锁，避免了死锁
        lock.lock();
        try {
            System.out.println("加锁了");
            Thread.sleep(3000);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            System.out.println("解锁了" + Thread.currentThread().getId());
            lock.unlock();
        }
        return R.ok().put("msg", Thread.currentThread().getName());
    }

    @GetMapping("/test3")
    public R test3() {
        List<Copy> copies = copyDao.selectList(null);
        ArrayList<ArticleEntity> lists = new ArrayList<>();
        for (Copy copy : copies) {
            ArticleEntity articleEntity = new ArticleEntity();
            articleEntity.setTime(DateConvertUtil.getDateTime(copy.getTime(), 1));
            articleEntity.setId(copy.getId());
            articleEntity.setContent(copy.getContent());
            articleEntity.setTitle(copy.getTitle());
            articleEntity.setUtime(DateConvertUtil.getDateTime(copy.getUtime(), 1));
            articleEntity.setViews(copy.getViews());
            articleEntity.setAuthor("1608881588@qq.com");
            articleEntity.setImg("http://tamad.fun:9001/images/2022/08/24/1219a2e9af874169a902f19f24bb5c85.jpg");
            // lists.add(articleEntity);
            lists.add(articleEntity);

        }
        articleService.saveBatch(lists);
        return R.ok().put("data", lists);
    }

    @GetMapping("/getOrders")
    public String getProducts() {
        return client.call();
    }

    @GetMapping("/test1")
    public R getTime() {
        Date dateTime = DateConvertUtil.getDateTime(1633357080, 1);
        return R.ok("成功将时间戳转换为时间").put("data", dateTime);
    }

    @GetMapping("/test2/{name}")
    public R sendEmails(@PathVariable("name") String name) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(name);
        rabbitTemplate.convertAndSend(EMAIL_EXCHANGE, userEntity);
        return R.ok("邮件发送成功").put("msg", "成功接收到消息");
    }

    // TODO 接下来的任务：大道
    @GetMapping("/testredis")
    public R testRedis() {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set("name", "maxuxu");
        System.out.println("成功存储");
        String name = ops.get("name");
        System.out.println("保存的数据是：" + name);
        return R.ok().put("msg", name);
    }
}
